package com.dianping.cat.alarm.rule.transform;

import com.dianping.cat.alarm.rule.IEntity;
import com.dianping.cat.alarm.rule.IVisitor;
import com.dianping.cat.alarm.rule.entity.Condition;
import com.dianping.cat.alarm.rule.entity.Config;
import com.dianping.cat.alarm.rule.entity.MetricItem;
import com.dianping.cat.alarm.rule.entity.MonitorRules;
import com.dianping.cat.alarm.rule.entity.Rule;
import com.dianping.cat.alarm.rule.entity.SubCondition;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
























public class DefaultJsonBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  46 */   public DefaultJsonBuilder() { this(false); }



/*  50 */   public DefaultJsonBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }


  public DefaultJsonBuilder(boolean compact, StringBuilder sb) {
/*  54 */     this.m_compact = compact;
/*  55 */     this.m_sb = sb;
/*  56 */     this.m_visitor = this;
  }

  protected void arrayBegin(String name) {
/*  60 */     indent();
/*  61 */     this.m_sb.append('"').append(name).append(this.m_compact ? "\":[" : "\": [\r\n");
/*  62 */     this.m_level++;
  }

  protected void arrayEnd(String name) {
/*  66 */     this.m_level--;

/*  68 */     trimComma();
/*  69 */     indent();
/*  70 */     this.m_sb.append("],").append(this.m_compact ? "" : "\r\n");
  }

  protected void attributes(Map<String, String> dynamicAttributes, Object... nameValues) {
/*  74 */     int len = nameValues.length;

/*  76 */     for (int i = 0; i + 1 < len; i += 2) {
/*  77 */       Object attrName = nameValues[i];
      Object attrValue = nameValues[i + 1];

/*  80 */       if (attrValue != null) {
/*  81 */         if (attrValue instanceof Collection) {

/*  83 */           Collection<Object> items = (Collection)attrValue;

/*  85 */           if (!items.isEmpty()) {
/*  86 */             indent();
/*  87 */             this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/*  89 */             for (Object item : items) {
/*  90 */               this.m_sb.append(' ');
/*  91 */               toString(this.m_sb, item);
/*  92 */               this.m_sb.append(',');
            }

/*  95 */             this.m_sb.setLength(this.m_sb.length() - 1);
/*  96 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }
/*  98 */         } else if (attrValue.getClass().isArray()) {
/*  99 */           int length = Array.getLength(attrValue);

/* 101 */           if (length > 0) {
/* 102 */             indent();
/* 103 */             this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/* 105 */             for (int j = 0; j < length; j++) {
/* 106 */               Object item = Array.get(attrValue, j);
/* 107 */               this.m_sb.append(' ');
/* 108 */               toString(this.m_sb, item);
/* 109 */               this.m_sb.append(',');
            }

/* 112 */             this.m_sb.setLength(this.m_sb.length() - 1);
/* 113 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }

/* 116 */         } else if (this.m_compact) {
/* 117 */           this.m_sb.append('"').append(attrName).append("\":");
/* 118 */           toString(this.m_sb, attrValue);
/* 119 */           this.m_sb.append(",");
        } else {
/* 121 */           indent();
/* 122 */           this.m_sb.append('"').append(attrName).append("\": ");
/* 123 */           toString(this.m_sb, attrValue);
/* 124 */           this.m_sb.append(",\r\n");
        }
      }
    }


/* 130 */     if (dynamicAttributes != null) {
/* 131 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 132 */         if (this.m_compact) {
/* 133 */           this.m_sb.append('"').append((String)e.getKey()).append("\":");
/* 134 */           toString(this.m_sb, e.getValue());
/* 135 */           this.m_sb.append(","); continue;
        }
/* 137 */         indent();
/* 138 */         this.m_sb.append('"').append((String)e.getKey()).append("\": ");
/* 139 */         toString(this.m_sb, e.getValue());
/* 140 */         this.m_sb.append(",\r\n");
      }
    }
  }


  public String build(IEntity<?> entity) {
/* 147 */     objectBegin(null);
/* 148 */     entity.accept(this);
/* 149 */     objectEnd(null);
/* 150 */     trimComma();

/* 152 */     return this.m_sb.toString();
  }

  public String buildArray(Collection<? extends IEntity<?>> entities) {
/* 156 */     this.m_sb.append('[');

/* 158 */     if (entities != null) {
/* 159 */       for (IEntity<?> entity : entities) {
/* 160 */         objectBegin(null);
/* 161 */         entity.accept(this);
/* 162 */         objectEnd(null);
      }

/* 165 */       trimComma();
    }

/* 168 */     this.m_sb.append(']');

/* 170 */     return this.m_sb.toString();
  }

  protected void indent() {
/* 174 */     if (!this.m_compact) {
/* 175 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 176 */         this.m_sb.append("   ");
      }
    }
  }

  protected void objectBegin(String name) {
/* 182 */     indent();

/* 184 */     if (name == null) {
/* 185 */       this.m_sb.append("{").append(this.m_compact ? "" : "\r\n");
    } else {
/* 187 */       this.m_sb.append('"').append(name).append(this.m_compact ? "\":{" : "\": {\r\n");
    }

/* 190 */     this.m_level++;
  }

  protected void objectEnd(String name) {
/* 194 */     this.m_level--;

/* 196 */     trimComma();
/* 197 */     indent();
/* 198 */     this.m_sb.append(this.m_compact ? "}," : "},\r\n");
  }

  protected void toString(StringBuilder sb, Object value) {
/* 202 */     if (value == null) {
/* 203 */       sb.append("null");
/* 204 */     } else if (value instanceof Boolean || value instanceof Number) {
/* 205 */       sb.append(value);
    } else {
/* 207 */       String val = value.toString();
/* 208 */       int len = val.length();

/* 210 */       sb.append('"');

/* 212 */       for (int i = 0; i < len; i++) {
/* 213 */         char ch = val.charAt(i);

/* 215 */         switch (ch) {
          case '"':
          case '/':
          case '\\':
/* 219 */             sb.append('\\').append(ch);
            break;
          case '\t':
/* 222 */             sb.append("\\t");
            break;
          case '\r':
/* 225 */             sb.append("\\r");
            break;
          case '\n':
/* 228 */             sb.append("\\n");
            break;
          default:
/* 231 */             sb.append(ch);
            break;
        }

      }
/* 236 */       sb.append('"');
    }
  }

  protected void trimComma() {
/* 241 */     int len = this.m_sb.length();

/* 243 */     if (this.m_compact) {
/* 244 */       if (len > 1 && this.m_sb.charAt(len - 1) == ',') {
/* 245 */         this.m_sb.replace(len - 1, len, "");
      }
    }
/* 248 */     else if (len > 3 && this.m_sb.charAt(len - 3) == ',') {
/* 249 */       this.m_sb.replace(len - 3, len - 2, "");
    }
  }



  public void visitCondition(Condition condition) {
/* 256 */     attributes(null, new Object[] { "minute", condition.getMinute(), "title", condition.getTitle(), "alertType", condition.getAlertType() });

/* 258 */     if (!condition.getSubConditions().isEmpty()) {
/* 259 */       arrayBegin("sub-conditions");

/* 261 */       for (SubCondition subCondition : condition.getSubConditions()) {
/* 262 */         objectBegin(null);
/* 263 */         subCondition.accept(this.m_visitor);
/* 264 */         objectEnd(null);
      }

/* 267 */       arrayEnd("sub-conditions");
    }
  }


  public void visitConfig(Config config) {
/* 273 */     attributes(null, new Object[] { "starttime", config.getStarttime(), "endtime", config.getEndtime() });

/* 275 */     if (!config.getConditions().isEmpty()) {
/* 276 */       arrayBegin("conditions");

/* 278 */       for (Condition condition : config.getConditions()) {
/* 279 */         objectBegin(null);
/* 280 */         condition.accept(this.m_visitor);
/* 281 */         objectEnd(null);
      }

/* 284 */       arrayEnd("conditions");
    }
  }



/* 290 */   public void visitMetricItem(MetricItem metricItem) { attributes(null, new Object[] { "monitorCount", metricItem.getMonitorCount(), "monitorSum", metricItem.getMonitorSum(), "monitorAvg", metricItem.getMonitorAvg(), "metricItemText", metricItem.getMetricItemText(), "productText", metricItem.getProductText() }); }




  public void visitMonitorRules(MonitorRules monitorRules) {
/* 296 */     if (!monitorRules.getRules().isEmpty()) {
/* 297 */       objectBegin("rules");

/* 299 */       for (Map.Entry<String, Rule> e : monitorRules.getRules().entrySet()) {
/* 300 */         String key = String.valueOf(e.getKey());

/* 302 */         objectBegin(key);
/* 303 */         ((Rule)e.getValue()).accept(this.m_visitor);
/* 304 */         objectEnd(key);
      }

/* 307 */       objectEnd("rules");
    }
  }


  public void visitRule(Rule rule) {
/* 313 */     attributes(rule.getDynamicAttributes(), new Object[] { "id", rule.getId() });

/* 315 */     if (!rule.getMetricItems().isEmpty()) {
/* 316 */       arrayBegin("metric-items");

/* 318 */       for (MetricItem metricItem : rule.getMetricItems()) {
/* 319 */         objectBegin(null);
/* 320 */         metricItem.accept(this.m_visitor);
/* 321 */         objectEnd(null);
      }

/* 324 */       arrayEnd("metric-items");
    }

/* 327 */     if (!rule.getConfigs().isEmpty()) {
/* 328 */       arrayBegin("configs");

/* 330 */       for (Config config : rule.getConfigs()) {
/* 331 */         objectBegin(null);
/* 332 */         config.accept(this.m_visitor);
/* 333 */         objectEnd(null);
      }

/* 336 */       arrayEnd("configs");
    }
  }



/* 342 */   public void visitSubCondition(SubCondition subCondition) { attributes(null, new Object[] { "type", subCondition.getType(), "text", subCondition.getText() }); }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/rule/transform/DefaultJsonBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */